/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.persistance;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;


import ar.com.wholeconsulting.pacc.model.Direccion;
import ar.com.wholeconsulting.pacc.model.Usuario;
import java.util.List;


/**
 * 
 * @author pable
 */
public class UsuariosParser {

	public static Usuario parseLine(String line, EntityManager manager) {
		// 3862;"DACOMAT S.A.";"SENZABELLO";"HORMIGON ELABORADO";1;"39171";"2145-0005408/2010-00";"30-68432519-8";"AV. SENZABELLO";"1800";"";"";"FLORENCIO VARELA";"1888";"FLORENCIO VARELA";"BUENOS AIRES";"4237-8549";"";""
		Usuario usr = new Usuario();
		try {
			line = line.replaceAll("\"", "");
			String[] vec = line.split(";");
			usr.setNombre(vec[1]);
			usr.setPlanta(vec[2]);
			usr.setRubro(vec[3]);
			usr.setExpediente(vec[6]);
			usr.setCuit(vec[7]);
			Direccion dir = new Direccion();
			dir.setCalle(vec[8]);
			dir.setNumero(vec[9]);
			dir.setPiso(vec[10]);
			dir.setOficina(vec[11]);
			dir.setLocalidad(vec[12]);
			dir.setCodigoPostal(vec[13]);
			dir.setPartido(vec[14]);
			dir.setProvincia(vec[15]);
			usr.setDireccion(dir);
			usr.setTelefono(vec[16]);
			usr.setEmail(vec[18]);
		} catch (ArrayIndexOutOfBoundsException e) {
			e.printStackTrace();
		}
		return usr;
	}

	public static void main(String[] args) throws Exception {
		// JPAUtil util = new JPAUtil();
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("PACC");
		EntityManager manager = factory.createEntityManager();
		EntityTransaction transaction = manager.getTransaction();
		transaction.begin();

		 Query query = manager.createNamedQuery(Usuario.FIND_ALL);
		 //query.setParameter("nombre", "DACOMAT S.A.");
		  List<Usuario> usuarios = query.getResultList();
		 //Usuario a = (Usuario) query.getSingleResult();
		  for (Usuario a : usuarios) {
		 System.out.println(a.getNombre());
		 System.out.println(a.getPlanta());
		 System.out.println(a.getRubro());
		 System.out.println("----------------------------------------");
		 }

//		try {
//			// Open the file that is the first
//			// command line parameter
//			FileInputStream fstream = new FileInputStream("/home/pable/NetBeansProjects/PACC/src/main/resources/base/Empresas.csv");
//			// Get the object of DataInputStream
//			DataInputStream in = new DataInputStream(fstream);
//			BufferedReader br = new BufferedReader(new InputStreamReader(in));
//			String strLine;
//			// Read File Line By Line
//			while ((strLine = br.readLine()) != null) {
//				// Print the content on the console
//				manager.persist(parseLine(strLine, manager));
//			}
//			// Close the input stream
//			in.close();
//		} catch (Exception e) {// Catch exception if any
//			System.err.println("Error: " + e.getMessage());
//		}
//
//		transaction.commit();

		manager.close();
		factory.close();
	}
}
